package com.nidu.demo.oauth2.executor.query;

import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import com.nidu.demo.oauth2.convertor.OAuth2RefreshTokenConvertor;
import com.nidu.demo.oauth2.dto.OAuth2RefreshTokenCO;
import com.nidu.demo.oauth2.gateway.OAuth2RefreshTokenGateway;
import com.nidu.demo.oauth2.model.OAuth2RefreshToken;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class OAuth2RefreshTokenSingleQryExe {

    private final OAuth2RefreshTokenGateway gateway;

    private final OAuth2RefreshTokenConvertor convertor;

    public SingleResponse<OAuth2RefreshTokenCO> execute(Long id) {
        OAuth2RefreshToken refreshToken = gateway.getById(id);
        if(refreshToken == null){
            throw new BizException(ErrorCodeConstants.OAUTH2_REFRESH_TOKEN_NOT_FOUND.getCode(), ErrorCodeConstants.OAUTH2_REFRESH_TOKEN_NOT_FOUND.getMessage());
        }
        return SingleResponse.of(convertor.toClientObject(refreshToken));
    }

}
